package com.suwish.pc;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.Properties;

import javax.swing.JOptionPane;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.suwish.device.util.Platform;
import com.suwish.pc.util.ResourceUtils;
import com.suwish.pc.util.StringUtils;

/**
 * 
 * 全局配置文件操作中心。</p>
 * 
 * 
 * 全局的配置文件分为两种，即XML和cfg。XML即标准的XML支持搜索等，由于结构规范
 * 使用起来比较灵活。<br/>
 * 
 * cfg为java的资源文件，使用KEY-VALUE方式存储，格式简单，存储内容限制比较大，
 * 但是读取和写入更加简单。
 * 
 * @author Min
 *
 */
public final class Setting {

//	private static SettingManager instance = null;
	
	private static Document document = null;
	private static Element root = null;
	
	private static Properties properties = null;
	
	private static boolean isLoad = false;
	
	private static final String XML_PATH = "configs/setting.xml";
	private static final String CFG_PATH = "configs" + File.separator + "setting.cfg";
	
//	public static synchronized SettingManager getInstance(){
//		return instance == null ? instance = new SettingManager() : instance;
//	}
	
	public static void initConfiguration() throws Exception{
		if(isLoad) return;
		initXML();
		initCfg();
		isLoad = true;
	}
	private static void initXML() throws Exception{
		try{
			URL url = ResourceUtils.loadResource(XML_PATH);
//			System.err.println("File:" + url.g);
//			System.err.println(url);
			document = new SAXReader().read(url);
			root = document.getRootElement();
		}catch(NullPointerException e){
//			File doc = new File("resources" + File.separator + "configs " + File.separator + "setting.xml");
//			System.out.println(doc.exists());
			JOptionPane.showMessageDialog(null, "resourcse/config/setting.xml not found exit now", "Error", JOptionPane.ERROR_MESSAGE);
			throw e;
		}
//		System.err.println(document.asXML());
	}
	
	private static void initCfg() throws Exception{
		properties = new Properties();
		try{
			properties.load(new FileInputStream(new File(Platform.getAppResourcesDir() + File.separator + CFG_PATH)));
		}catch(NullPointerException e){
			JOptionPane.showMessageDialog(null, "resources/configs/setting.cfg not found exit now", "Error", JOptionPane.ERROR_MESSAGE);
			throw e;
		}
	}
	
	public static  void saveConfiguration(){
		try{
			properties.store(new FileOutputStream(new File(Platform.getAppResourcesDir() + File.separator + CFG_PATH)),
					"Java Phone Bridge Config file");
		}catch(Exception ex){
			ex.printStackTrace();
		}
//		try{
//			XMLWriter writer = new XMLWriter(new FileOutputStream(
//					new File(ResourceUtils.loadResource("configs" + File.separator + "setting.cfg").toURI())));
//			writer.write(document);
//			writer.close();
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}
//		new 
	}
	
	/**
	 * 
	 * @param key
	 * @param def
	 * @return
	 * 
	 */
	public String load(String key, String def){
		
		return key;
	}
	
	public static String get(String key, String def){
		String value =  get(key);
		return StringUtils.isEmpty(value) ? def : value;
	}
	
	public static String get(String key){
		return properties.getProperty(key);
	}
	/**
	 * get and store where value  is null
	 * 
	 * @param key
	 * @param def
	 * @return
	 */
	public static String _get(String key, String def){
		String value =  properties.getProperty(key);
		if(StringUtils.isEmpty(value)){
			set(key, value);
		}
		return StringUtils.isEmpty(value) ? def : value;
	}
	public static  void set(String key, String value){
		properties.setProperty(key, value);
	}
	/**
	 * 
	 * 测试给定的KEY对应的值是否是给定的期望值
	 * @param key
	 * @param value
	 * @return
	 */
	public static boolean isTarget(String key, String value){
		return StringUtils.isEqual(get(key), value);
	}
}
